<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" placeholder="用户名" :focus="isFocus"></wInput>
				<wInput v-model="passData" type="password"  placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>

			<!-- 底部信息 -->
			<view class="footer">
				<!-- <navigator url="forget" open-type="navigate">找回密码</navigator> -->
				<!-- <text>|</text> -->
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
import { navigateBack } from '../../static/utils';

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {						
			this.isLogin()
		},
		methods: {
			isLogin() {
				// 判断缓存中是否登录过， 直接登录
				try {
					const value = uni.getStorageSync('userInfo');
					if (value) {
						console.log(value)
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
				} catch (e) {
					// error
				}
			},
			startLogin(e) {
				console.log(e)
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				console.log(this.phoneData,this.passData);
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}else{
					this.isRotate = true
					uni.showLoading({
						title: '登录中'
					});
				}
				uni.request({
					url: 'https://fc-mp-fff067d4-0ad0-46f1-8940-3076faaf2173.next.bspapp.com/login',
					data: {
						phoneData: this.phoneData,
						passData: this.passData,
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							try {
								uni.setStorageSync('userInfo', res.data.userInfo);
							} catch (e) {
								// error
							}
							uni.showToast({
								icon: 'success',
								title: '登录成功'
							});
							setTimeout(()=>{
								navigateBack(1)
							},500)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.mesg
							});
						}
					},
					complete: () => {
						uni.hideLoading();
						this.isRotate = false
					}
				})

			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
